package cn.xuewenbao.edu.service.impl;

import cn.xuewenbao.edu.entity.ViewContent;
import cn.xuewenbao.edu.mapper.ViewContentMapper;
import cn.xuewenbao.edu.service.ViewContentService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.api.vo.Result;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author mafayu
 * @since 2021-09-06
 */
@Service
public class ViewContentServiceImpl extends ServiceImpl<ViewContentMapper, ViewContent> implements ViewContentService {

    @Override
    public Result<Map<String, Long>> getLastViewCount(String userId) {
        Result<Map<String, Long>> result = new Result<>();
        //获取当前日期
        Calendar calendar = Calendar.getInstance();
        //得到前一天
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String lastDay = formatter.format(calendar.getTime());
        String start = "2021-01-01 00:00:00";
        String end = lastDay + " 23:59:59";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        QueryWrapper<ViewContent> wrapper = new QueryWrapper<>();
        try {
            wrapper.between("gmt_create", sdf.format(sdf.parse(start)), sdf.format(sdf.parse(end)));
            wrapper.eq("author_id", userId);
        } catch (ParseException e) {
            log.error(e.toString());
        }
        Map<String, Long> map = new HashMap<>();
        map.put(lastDay, Long.valueOf(baseMapper.selectCount(wrapper)));
        result.setResult(map);
        return result;
    }

    @Override
    public Result<Map<String, Long>> getWeekViewCount(String userId) {
        Result<Map<String, Long>> result = new Result<>();
        Map<String, Long> map = new HashMap<>();
        //获取当前日期
        Calendar calendar = Calendar.getInstance();
        //得到一周的点赞量
        for (int i = 1; i <= 7; i++) {
            calendar.add(Calendar.DAY_OF_MONTH, -1);
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            String lastDay = formatter.format(calendar.getTime());
            String start = "2021-01-01 00:00:00";
            String end = lastDay + " 23:59:59";
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            QueryWrapper<ViewContent> wrapper = new QueryWrapper<>();
            try {
                wrapper.between("gmt_create", sdf.format(sdf.parse(start)), sdf.format(sdf.parse(end)));
                wrapper.eq("author_id", userId);
            } catch (ParseException e) {
                log.error(e.toString());
            }
            map.put(lastDay, Long.valueOf(baseMapper.selectCount(wrapper)));
        }
        result.setResult(map);
        return result;
    }

    @Override
    public Result<Map<String, Long>> getMonthViewCount(String userId) {
        Result<Map<String, Long>> result = new Result<>();
        Map<String, Long> map = new HashMap<>();
        //获取当前日期
        Calendar calendar = Calendar.getInstance();
        //得到一周的点赞量
        for (int i = 1; i <= 30; i++) {
            calendar.add(Calendar.DAY_OF_MONTH, -1);
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            String lastDay = formatter.format(calendar.getTime());
            String start = "2021-01-01 00:00:00";
            String end = lastDay + " 23:59:59";
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            QueryWrapper<ViewContent> wrapper = new QueryWrapper<>();
            try {
                wrapper.between("gmt_create", sdf.format(sdf.parse(start)), sdf.format(sdf.parse(end)));
                wrapper.eq("author_id", userId);
            } catch (ParseException e) {
                log.error(e.toString());
            }
            map.put(lastDay, Long.valueOf(baseMapper.selectCount(wrapper)));
        }
        result.setResult(map);
        return result;
    }

    //得到该作者总阅读量
    @Override
    public Result<Integer> getTotalViewCount(String userId) {
        Result<Integer> result = new Result<>();
        QueryWrapper<ViewContent> wrapper = new QueryWrapper<>();
        wrapper.eq("author_id", userId);
        result.setResult(baseMapper.selectCount(wrapper));
        return result;
    }

}
